package cn.yunhe.controller;

import cn.yunhe.model.Role;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

/**
 * Created by zs on 2017/12/15.
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @RequestMapping("/addRole")
    public String addRole(Model model, String roleName, String note) {
        model.addAttribute("roleName", roleName);
        model.addAttribute("note", note);
        return "redirect:showRoleJsonInfo.do";
    }

    @RequestMapping("/showRoleJsonInfo")
    public ModelAndView showRoleJsonInfo(String roleName, String note) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("roleName", roleName);
        mv.addObject("note", note);
        return mv;

    }

    @RequestMapping("/addRole2")
    public String addRole2(RedirectAttributes ra, Role role) {
        System.out.println("控制器被执行了");
        ra.addFlashAttribute("role", role);
        return "redirect:showRoleJsonInfo2.do";
    }

    @RequestMapping("/showRoleJsonInfo2")
    public ModelAndView showRoleJsonInfo2(Role role) {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        mv.addObject("role", role);
        return mv;
    }


    @RequestMapping("/addInte")
    public ModelAndView addInte(@RequestParam(value = "age",required = false) Integer id) {
        ModelAndView mv = new ModelAndView();
        System.out.println(id);
        mv.setViewName("hello");
        return mv;
    }


}
